package com.redtrusssoftware.truckloaderpro;

import android.net.ParseException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.redtrusssoftware.truckloaderpro.R;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		Parse.initialize(this, "fYPxyNR0IV4bJsKnupZpLJnvurlXfXDjltYSmWRF", "AySq0ABtcNNlNYWCNoKv0GK95XsRil834GOXgucJ"); 
		ParseUser currentUser = ParseUser.getCurrentUser();
		if(currentUser != null){
			//Toast.makeText(getApplicationContext(), currentUser.getEmail(),Toast.LENGTH_SHORT ).show();
			startEventController();
		}else {
			login(findViewById(R.id.mainview));
		}
	
	}

	public void login(View view){
		EditText loginField = (EditText)findViewById(R.id.login);
		EditText passwordField = (EditText)findViewById(R.id.test);
		final String username = loginField.getText().toString();
		final String password = passwordField.getText().toString();
		//Toast.makeText(getApplicationContext(), "successful", Toast.LENGTH_SHORT).show();
		ParseUser.logInInBackground(username, password, new LogInCallback() {
			@Override
			public void done(ParseUser user, com.parse.ParseException e) {
				// TODO Auto-generated method stub
				if (user != null) {
				      Toast.makeText(getApplicationContext(), "login successful", Toast.LENGTH_SHORT).show();
				    } else {
				      // Signup failed. Look at the ParseException to see what happened.
				    	String response = e.getMessage();
				    	Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
				    }			
			}
			});
		
		//startActivity(intent2);
		startEventController();
			
	}

	public void beginSignUp(View view){
		Intent intent = new Intent(getApplicationContext(),SignUp_Activity.class);
		startActivity(intent);
	}
	
	
	public void startEventController(){
		Intent intent2 = new Intent(getApplicationContext(),NewEventController.class);
		startActivity(intent2);

	}
	
		
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
